package cc.dawning.utiming.lklocation.location.utils;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import cc.dawning.utiming.lklocation.location.view.WindowBottomDialog;


/**
 * Date 2018/3/28
 * Describe 设置搜索界面
 *
 * @auther liuxin
 */

public class LocationSearchDialogUtils {


    /**
     * 从界面底部弹出的填充屏幕宽度的Dialog
     *
     * @param activity 上下文
     * @param view     Dialog布局
     * @return dialog
     */
    public static Dialog getBottomDialog(Activity activity, View view) {
        WindowBottomDialog windowBottomDialog = new WindowBottomDialog(activity, 0);
        windowBottomDialog.setBottomView(view);
        windowBottomDialog.setCanceledOnTouchOutside(true);
        return windowBottomDialog;
    }

    /**
     * 点击非dialog区域，dialog消失
     * 宽度铺满全屏 弹出位置位于整个屏幕居中显示
     *
     * @param activity
     * @param view
     * @param style
     * @param anim
     * @return dialog
     */
    public static Dialog getCenterDialog(Activity activity, View view, int style, int anim) {
        Dialog dialog = new Dialog(activity, style);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = dialog.getWindow();
        if (anim != 0) {
            Animation animation = AnimationUtils.loadAnimation(activity, anim);
            view.setAnimation(animation);
        }
        //获取view的高度和宽度
        int width = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        // 获取高度
        int viewHeight = view.getMeasuredHeight();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.y = activity.getWindowManager().getDefaultDisplay().getHeight() / 2 - (viewHeight / 2);
        wl.gravity = Gravity.BOTTOM;
        dialog.onWindowAttributesChanged(wl);
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }

}
